{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:02:16.362420Z",
     "start_time": "2019-12-29T16:02:15.929689Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:16:17.960780Z",
     "start_time": "2019-12-29T16:16:17.954796Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0, 0],\n",
       "        [1, 0],\n",
       "        [2, 0],\n",
       "        [0, 2]]), array([ 1,  1, -1, -1]))"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.asarray([[0,0],[1,0],[2,0],[0,2]])\n",
    "y = np.asarray([1,1,-1,-1])\n",
    "x,y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:02:17.117875Z",
     "start_time": "2019-12-29T16:02:16.962190Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1d5af1d1dd8>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAXY0lEQVR4nO3dfZRddX3v8feHmclQkEpCRsU8kOBNVfDy5GlEcSm0NQSuJVpdy+TaGiyuFCu12rt6L5ZVaUPvKvd6V7W2tJh6c9G2JrQIdtolYhS8XOUGc0LDQ8DAGNBMwyIj4bHBJJP59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63KdvBkvcg+oX6ULDivydvWkp0pAxwP/H1+8H8fOL2w7DX5cjuASyaxpm8BTwHb8ld/3v424MH8gH8QuGKS99UfA9vz7d8FvKGw7K/n+3AA+PBk1pXP/wFwfcNyHdtfZGd4TwIHyc6krgCuBK7M+wXckNf8IFCbpH3Vrq4vAs8Ujq163n56vp/uz3/G10xiTVcVjqvNFN6Emv3sJ6uufMzlZDdlFJfr5L56O9nllgcKP6NLJ/vY8n+BYGaWuJl2jd7MzMbJQW9mljgHvZlZ4hz0ZmaJc9CbmSXOQW9mljgHvZlZ4v4NW3zmU6u/vI8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x[:,0],x[:,1],c=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:02:17.550245Z",
     "start_time": "2019-12-29T16:02:17.545258Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1],\n",
       "       [ 1],\n",
       "       [-1],\n",
       "       [-1]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = y.reshape(-1,1)\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一种用numpy的做法 $A(\\alpha)=\\sum_{i=1}^{n}\\alpha_i - \\frac{1}{2}\\sum_{i=1}^{n}\\sum_{j=1}^{n}\\alpha_i\\alpha_j y_i y_j x_i^T x_j$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:02:18.965523Z",
     "start_time": "2019-12-29T16:02:18.961534Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 1, 2, 0],\n",
       "       [0, 2, 4, 0],\n",
       "       [0, 0, 0, 4]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xij = x.dot(x.T)\n",
    "xij"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:02:19.654163Z",
     "start_time": "2019-12-29T16:02:19.650174Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  1, -1, -1],\n",
       "       [ 1,  1, -1, -1],\n",
       "       [-1, -1,  1,  1],\n",
       "       [-1, -1,  1,  1]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yij = y.dot(y.T)\n",
    "yij"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:02:20.573833Z",
     "start_time": "2019-12-29T16:02:20.568818Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0],\n",
       "       [ 0,  1, -2,  0],\n",
       "       [ 0, -2,  4,  0],\n",
       "       [ 0,  0,  0,  4]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = yij*xij\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:05:43.740117Z",
     "start_time": "2019-12-29T16:05:43.735157Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 a1^2\n",
      "-4 a1a2\n",
      "4 a2^2\n",
      "4 a3^2\n"
     ]
    }
   ],
   "source": [
    "for i in range(A.shape[0]):\n",
    "    for j in range(i,A.shape[1]):\n",
    "        if i == j:\n",
    "            coef = A[i][i]\n",
    "            if coef !=0:\n",
    "                print(f\"{coef} a{i}^2\")\n",
    "        else:\n",
    "            coef = A[i][j] + A[j][i]            \n",
    "            if coef !=0:\n",
    "                print(f\"{coef} a{i}a{j}\")\n",
    "# 手算求导下去"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第二种sympy符号计算 $A(\\alpha)=\\sum_{i=1}^{n}\\alpha_i - \\frac{1}{2}\\sum_{i=1}^{n}\\sum_{j=1}^{n}\\alpha_i\\alpha_j y_i y_j x_i^T x_j$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:15:46.179779Z",
     "start_time": "2019-12-29T16:15:46.174754Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{1}$"
      ],
      "text/plain": [
       "a_1"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sympy\n",
    "a = sympy.symbols(\"a_1 a_2 a_3 a_4\") \n",
    "a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:22:03.224576Z",
     "start_time": "2019-12-29T16:22:03.219589Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{1} + a_{2} + a_{3} + a_{4}$"
      ],
      "text/plain": [
       "a_1 + a_2 + a_3 + a_4"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eq1 = sympy.Add(*a)\n",
    "eq1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:21:03.291745Z",
     "start_time": "2019-12-29T16:21:03.283740Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{2}^{2} - 4 a_{2} a_{3} + 4 a_{3}^{2} + 4 a_{4}^{2}$"
      ],
      "text/plain": [
       "a_2**2 - 4*a_2*a_3 + 4*a_3**2 + 4*a_4**2"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eq = []\n",
    "for i in range(x.shape[0]):\n",
    "    for j in range(x.shape[0]):\n",
    "        eq.append(a[i]*a[j]*y[i]*y[j]*(x[i]*x[j]).sum())\n",
    "eq2 = sympy.Add(*eq)\n",
    "eq2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:22:57.594102Z",
     "start_time": "2019-12-29T16:22:57.587120Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{1} - 0.5 a_{2}^{2} + 2.0 a_{2} a_{3} + a_{2} - 2.0 a_{3}^{2} + a_{3} - 2.0 a_{4}^{2} + a_{4}$"
      ],
      "text/plain": [
       "a_1 - 0.5*a_2**2 + 2.0*a_2*a_3 + a_2 - 2.0*a_3**2 + a_3 - 2.0*a_4**2 + a_4"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eq = eq1-1/2*eq2\n",
    "eq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:23:15.992367Z",
     "start_time": "2019-12-29T16:23:15.987353Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sympy.diff(eq,\"a_1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:23:54.250999Z",
     "start_time": "2019-12-29T16:23:54.244936Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - 1.0 a_{2} + 2.0 a_{3} + 1$"
      ],
      "text/plain": [
       "-1.0*a_2 + 2.0*a_3 + 1"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sympy.diff(eq,\"a_2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:24:01.777701Z",
     "start_time": "2019-12-29T16:24:01.763204Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2.0 a_{2} - 4.0 a_{3} + 1$"
      ],
      "text/plain": [
       "2.0*a_2 - 4.0*a_3 + 1"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sympy.diff(eq,\"a_3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:24:09.358919Z",
     "start_time": "2019-12-29T16:24:09.348974Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1 - 4.0 a_{4}$"
      ],
      "text/plain": [
       "1 - 4.0*a_4"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sympy.diff(eq,\"a_4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:30:16.193067Z",
     "start_time": "2019-12-29T16:30:16.186060Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{1} + a_{2} - a_{3} - a_{4}$"
      ],
      "text/plain": [
       "a_1 + a_2 - a_3 - a_4"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sympy.Add(*[y[i]*a[i] for i in range(len(a))])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 求 $\\alpha_i$\n",
    "约束包括：\n",
    "- $\\sum_{i=1}^{n} y_i \\alpha_i=0$\n",
    "- $\\frac{\\partial A(\\alpha)}{\\partial\\alpha_i}=0$\n",
    "\n",
    "中间过程不等式约束需要手算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:30:30.402501Z",
     "start_time": "2019-12-29T16:30:30.395492Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " -1.0*a_2 + 2.0*a_3 + 1,\n",
       " 2.0*a_2 - 4.0*a_3 + 1,\n",
       " 1 - 4.0*a_4,\n",
       " a_1 + a_2 - a_3 - a_4]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin = []\n",
    "for i in range(len(a)):\n",
    "    lin.append(sympy.diff(eq,a[i]))\n",
    "lin.append(sympy.Add(*[y[i]*a[i] for i in range(len(a))]))\n",
    "lin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:35:51.371068Z",
     "start_time": "2019-12-29T16:35:51.366054Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1 - 4.0 a_{4}$"
      ],
      "text/plain": [
       "1 - 4.0*a_4"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lin[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T16:36:08.795086Z",
     "start_time": "2019-12-29T16:36:08.790101Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{4}$"
      ],
      "text/plain": [
       "a_4"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-30T02:38:34.242292Z",
     "start_time": "2019-12-30T02:38:33.951572Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\left( 0.75 - 1.0 a_{3}, \\  2.0 a_{3} - 0.5, \\  a_{3}, \\  0.25\\right)\\right\\}$"
      ],
      "text/plain": [
       "FiniteSet((0.75 - 1.0*a_3, 2.0*a_3 - 0.5, a_3, 0.25))"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sympy.linsolve(lin[2:],a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T17:33:22.429232Z",
     "start_time": "2019-12-29T17:33:22.424246Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32, 0.33, 0.34, 0.35,\n",
       "       0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43, 0.44, 0.45, 0.46,\n",
       "       0.47, 0.48, 0.49, 0.5 , 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57,\n",
       "       0.58, 0.59, 0.6 , 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68,\n",
       "       0.69, 0.7 , 0.71, 0.72, 0.73, 0.74, 0.75])"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_3 = np.arange(0,100,0.01)\n",
    "a_3[(0.75 - 1.0*a_3>=0) & (2.0*a_3 - 0.5 >=0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-30T02:38:37.377774Z",
     "start_time": "2019-12-30T02:38:37.350121Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\left( - 1.0 a_{3} - 0.75, \\  2.0 a_{3} + 1.0, \\  a_{3}, \\  0.25\\right)\\right\\}$"
      ],
      "text/plain": [
       "FiniteSet((-1.0*a_3 - 0.75, 2.0*a_3 + 1.0, a_3, 0.25))"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sympy.linsolve([lin[1],lin[3],lin[4]],a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-29T17:33:17.973401Z",
     "start_time": "2019-12-29T17:33:17.968385Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([], dtype=float64)"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_3 = np.arange(0,100,0.01)\n",
    "a_3[(-1.0*a_3 - 0.75>=0) & (2.0*a_3 + 1.0 >=0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-30T03:10:12.477808Z",
     "start_time": "2019-12-30T03:10:12.472794Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.5, 0, 0.25, 0.25]"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不等式约束，取一个合法值，手算得出，具体取谁还是要看图说话\n",
    "a = [0.5,0,0.25,0.25]\n",
    "# a = [0.0,1.0,3/4,1/4]\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算参数 $W^{*}=\\sum_{i=1}^{n}\\alpha_i y_i x_i$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-30T03:10:13.360862Z",
     "start_time": "2019-12-30T03:10:13.355875Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.5, -0.5])"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w = 0\n",
    "for i in range(x.shape[0]):\n",
    "        w += a[i]*y[i]*x[i]\n",
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算参数b\n",
    "$a_i$的值大于0，对应于样本$i$是支持向量\n",
    "\n",
    "从支持向量的约束条件： $yi * ( W * Xi + b ) – 1 = 0 $\n",
    "\n",
    "当 $y_i = +1$ 类时，有 $b^* =   1- W *  X_i$                 \n",
    "\n",
    "当 $y_j = - 1$ 类时， 有 $b^* =  - 1- W * X_j$   \n",
    "\n",
    "求和取平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-12-30T03:11:40.701761Z",
     "start_time": "2019-12-30T03:11:40.696742Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([array([1., 1.]), array([0., 0.])], 0.5)"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = []\n",
    "for i in range(len(a)-1):\n",
    "    if a[i]>0:\n",
    "        b.append(y[i]-w*x[i].sum())\n",
    "b,np.mean(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow",
   "language": "python",
   "name": "tensorflow"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
